<template>
  <content-box>
    <div class="box-title" slot="header" style="width: 100%;padding-left:4px;">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="append()">{{$t('btns.add')}}</el-button>
      <el-button type="danger" size="small" icon="el-icon-delete" @click="remove">{{$t('btns.delete')}}</el-button>
    </div>

    <el-row slot="body" class="tree-content-box" :gutter="24">
      <el-col :span="6" :xs="24" :sm="24" :md="6" :lg="6">
        <el-card class="box-card" body-style="padding: 0px;">
          <div class="text item">
            <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick" :expand-on-click-node="false"
              show-checkbox node-key="id">
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <span>
                  <el-tooltip :content="$t('btns.add')+$t('btns.children')+$t('btns.department')" placement="top">
                    <svg-icon icon-class="plusRound" @iconClick="append(data.id, data.children)"/>
                  </el-tooltip>
                  <el-tooltip :content="$t('btns.config')+$t('btns.role')" placement="top">
                    <svg-icon icon-class="wrench" @iconClick="settingResource($event, data.id)"/>
                  </el-tooltip>
                  <el-tooltip :content="$t('btns.delete')+$t('btns.department')" placement="top">
                    <svg-icon icon-class="del" @iconClick="remove(data.id)"/>
                  </el-tooltip>
                </span>
              </span>
            </el-tree>
          </div>
        </el-card>
      </el-col>

      <!-- 表单 -->
      <el-col :span="18" :xs="24" :sm="24" :md="18" :lg="18">
        <el-card class="box-card">
          <div class="text item">
            <el-form :model="form" ref="roleForm" :rules="rules">
              <el-form-item label="父级" :label-width="formLabelWidth">
                <el-select-tree v-model="form.parentId" :treeData="treeData" :propNames="defaultProps" clearable
                                placeholder="请选择父级">
                </el-select-tree>
              </el-form-item>

              <el-form-item label="名称" :label-width="formLabelWidth">
                <el-input v-model="form.name" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="英文" :label-width="formLabelWidth">
                <el-input v-model="form.enName" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="是否生效" :label-width="formLabelWidth">
                <el-radio class="radio" v-model="form.usable" border label="1">是</el-radio>
                <el-radio class="radio" v-model="form.usable" border label="0">否</el-radio>
              </el-form-item>
              <el-form-item label="排序" :label-width="formLabelWidth">
                <el-input-number :min="0" v-model="form.sort"></el-input-number>
              </el-form-item>
              <el-form-item label="" :label-width="formLabelWidth">
                <el-button type="primary" @click="onSubmit" v-text="form.id?'修改':'新增'"></el-button>
                <el-button type="info" @click="settingResource($event,form.id)" icon="setting" v-if="form.id && form.id!=null">
                  {{$t('btns.config') + $t('btns.role')}}
                </el-button>
                <el-button type="danger" @click="deleteSelected" icon="delete" v-if="form.id && form.id!=null">删除
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>

        <!-- 权限设置 -->
        <el-dialog class="dialog-select-tree" :title="$t('btns.config') + $t('btns.role') + dialogTitle" 
        :visible.sync="dialogVisible" width="30%">
          <div class="select-tree">
          <el-scrollbar
            tag="div"
            class='is-empty'
            wrap-class="el-select-dropdown__wrap"
            view-class="el-select-dropdown__list">
          <el-tree
            :data="roleData"
            ref="resourceTree"
            show-checkbox
            check-strictly
            node-key="id"
            v-loading="dialogLoading"
            :props="defaultProps">
          </el-tree>
          </el-scrollbar>
          </div>
          <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="configRoleResources">确 定</el-button>
          </span>
        </el-dialog>

      </el-col>
    </el-row>
  </content-box>
</template>

<script>
  import ContentBox from '@/views/layout/components/ContentBox'
  import TreeSelect from '@/components/TreeSelect'
  import treeter from '@/components/TreeSelect/treeter'
  import { getDepartmentList, departmentAdd, departmentDel } from '@/api/department'
  import { getRoleList } from '@/api/role'
  import '@/styles/tree-menus.scss'

  export default {
    mixins: [treeter],
    components: {
      ContentBox,
      'el-select-tree': TreeSelect
    },
    data() {
      return {
        dialogTitle: '',
        dialogLoading: false,
        dialogVisible: false,
        formLabelWidth: '100px',
        treeData: [],
        roleData: [],
        rules: {
          name: [{ required: true, trigger: 'blur' }],
          enName: [{ required: true, trigger: 'blur' }]
        },
        defaultProps: {
          children: 'children',
          label: 'name',
          id: 'id'
        },
        form: {
          id: null,
          parentId: null,
          name: '',
          enName: '',
          sort: 0,
          usable: '1'
        }
      }
    },

    methods: {
      // 新增按钮
      append(id, children) {
        children = children || []
        children = children.length + 1
        this.form = {
          id: null,
          parentId: id || null,
          name: '',
          enName: '',
          sort: children,
          usable: '1'
        }
      },
      handleNodeClick(data) {
        this.form = data
      },
      // 删除角色
      remove(id) {
        console.log('我的删除！！！！！', id)
        departmentDel({
          id: id
        }).then(res => {
          this.$message({ message: this.$t('btns.delete') + this.$t('msgs.success'), type: 'success' })
          console.log('xxxxxxxxxxx', this.deleteFromTree(this.treeData, id))
          this.append()
          // this.load()
        })
      },
      // 提交表单
      onSubmit() {
        this.form.parentId = this.form.parentId
        this.$refs.roleForm.validate(valid => {
          if (valid) {
            departmentAdd(this.form).then(res => {
              console.log('操作结果', res, this.form)
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteSelected(id) {
      },
      // 设置配置
      settingResource(event, id) {
        console.log('设置权限', id, event)
        // event.stopPropagation()
        this.dialogVisible = true
      },
      // 权限保存
      configRoleResources() {
      },
      // 加载数据
      load() {
        getDepartmentList().then(res => {
          this.treeData = []
          this.treeData.push(...res.data)
        })
        // 获取角色列表
        getRoleList().then(res => {
          this.roleData = []
          this.roleData.push(...res.data)
        })
      }
    },
    created() {
      // 初始化数据
      this.load()
    }
  }
</script>
